package com.lcj.dr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lcj.common.util.Result;
import com.lcj.dr.controller.form.DriverRechargeForm;
import com.lcj.dr.controller.form.TransferForm;
import com.lcj.dr.controller.form.WalletPageForm;
import com.lcj.dr.db.pojo.WalletEntity;
import com.lcj.dr.db.pojo.WalletIncomeEntity;
import com.lcj.dr.service.WalletIncomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/wallet/income")
@Tag(name = "WalletIncomeController", description = "司机钱包入账Web接口")
public class WalletIncomeController {

    @Resource
    WalletIncomeService walletIncomeService;

    @PostMapping("/searchWalletIncomeByPage")
    @Operation(summary = "分页查询钱包入账数据")
    public Result searchWalletIncomeByPage(@RequestBody @Valid WalletPageForm walletPageForm) {
        PageInfo<WalletEntity> pageInfo = walletIncomeService.searWalletIncomeByPage(walletPageForm);
        return Result.ok().put("data", pageInfo);
    }

    @PostMapping("/searchWalletPaymentByPage")
    public Result searchWalletPaymentByPage(@RequestBody @Valid WalletPageForm walletPageForm) {

        return Result.ok();
    }

    @PostMapping("/transfer")
    @Operation(summary = "转账")
    public Result transfer(@RequestBody @Valid TransferForm form) {
        WalletIncomeEntity entity = BeanUtil.toBean(form, WalletIncomeEntity.class);
        entity.setStatus((byte) 3);
        int rows = walletIncomeService.transfer(entity);
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/transfers")
    @Operation(summary = "批量转账")
    public Result transfers(@RequestBody List<@Valid TransferForm> list) {
        int rows = 0;
        for(int i=0;i<list.size();i++) {
            WalletIncomeEntity entity = BeanUtil.toBean(list.get(i), WalletIncomeEntity.class);
            entity.setStatus((byte) 3);
            rows += walletIncomeService.transfer(entity);
        }

        return Result.ok().put("rows", rows);
    }

    @PostMapping("/recharge")
    @Operation(summary = "充值")
    public Result recharge(@RequestBody DriverRechargeForm form) {
        int rows = walletIncomeService.recharge(form);
        return Result.ok().put("rows", rows);
    }
}